package com.cykj.page06;

/**
 * @BelongsProject: HandTomcat
 * @BelongsPackage: com.cykj.page06
 * @Author: 阿隆
 * @CreateTime: 2025-11-15 11:08
 * @Description: TODO
 * @Version: 1.0
 */
public class TicketRunnable implements Runnable {
    //总票数
    private int ticket = 100;

    // 线程安全问题: 多条线程抢同一个资源

    @Override
    public synchronized void run() {

        while (true) {
            if (ticket <= 0) {
                System.out.println("票卖完了......");
                break;
            } else {

                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

                ticket--;
                System.out.println("当前的线程为: " + Thread.currentThread().getName() + " 当前的票还有 " + ticket + "张");
            }
        }


    }


//    @Override
//    public void run() {
//
//        //同步代码块
//        synchronized (this) {
//            while (true) {
//                if (ticket <= 0) {
//                    System.out.println("票卖完了......");
//                    break;
//                } else {
//
//                    try {
//                        Thread.sleep(50);
//                    } catch (InterruptedException e) {
//                        throw new RuntimeException(e);
//                    }
//
//                    ticket--;
//                    System.out.println("当前的线程为: " + Thread.currentThread().getName() + " 当前的票还有 " + ticket + "张");
//                }
//            }
//        }
//
//    }


}